<?php
function dpa_unlock_achievement_notification( $user_id, $achievement_name ) {
		global $bp, $wpdb;

		if ( 'no' == get_usermeta( (int) $user_id, 'notification_dpa_unlock_achievement' ) )
			return false;

		$sql = $wpdb->prepare( "SELECT nice_name FROM {$bp->achievements->table_achievements} WHERE short_name = %s LIMIT 1", $achievement_name );
		$userdata = get_userdata( $user_id );
		$profile_url = bp_core_get_userurl( $user_id );
		$subject = sprintf( __( '[%s] Achievement unlocked', 'dpa' ), get_blog_option( BP_ROOT_BLOG, 'blogname' ) );

		$message = sprintf( __( '
	You have unlocked an achievement: %s

	To review this and see all of your achievements, go to %s

	---------------------
	', 'dpa' ), apply_filters( 'dpa_filter_achievement_nice_name', $wpdb->get_var( $sql ) ), $profile_url . $bp->achievements->slug );

		$message .= sprintf( __( 'To disable these notifications please log in and go to: %s', 'dpa' ), $profile_url . $bp->settings->slug . '/notifications' );

	wp_mail( apply_filters( 'dpa_unlock_achievement_email', $userdata->user_email ), apply_filters( 'dpa_unlock_achievement_email_subject', $subject ), apply_filters( 'dpa_unlock_achievement_email_message', $message ) );
}
add_action( 'dpa_unlock_achievement_notification', 'dpa_unlock_achievement_notification', 10, 2 );
?>